package com.kurumi.util;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PageIndexCheckUtil {

	public static Map<String,String> checkHQMS1(String sexName,String diseaseDiagCode){
		Map<String,String> result =new HashMap<String, String>();
		List<String> diagCodes = new ArrayList<String>();
		diagCodes.add("B26.0");
		diagCodes.add("C60");
		diagCodes.add("C61");
		diagCodes.add("C62");
		diagCodes.add("C63");
		diagCodes.add("D07.4");
		diagCodes.add("D07.5");
		diagCodes.add("D07.6");
		diagCodes.add("D17.6");
		diagCodes.add("D29");
		diagCodes.add("D40");
		diagCodes.add("E29");
		diagCodes.add("E89.5");
		diagCodes.add("F52.4");
		diagCodes.add("I86.1");
		diagCodes.add("L29.1");
		for(int i= 40;i<=51;i++){
			diagCodes.add("N"+i);
		}
		diagCodes.add("Q53");
		diagCodes.add("Q54");
		diagCodes.add("Q55");
		diagCodes.add("R86");
		diagCodes.add("S31.2");
		diagCodes.add("S31.3");
		diagCodes.add("Z12.5");
		if(sexName.contains("女")){
			for (String diagCode : diagCodes) {
				if(diseaseDiagCode.toUpperCase().startsWith(diagCode)){
					String errorCode = "error1";
					String errorItem = "性别（男性）/疾病诊断ICD10编码";
					String errorDesc = "只适用于男性：B26.0；C60-C63；D07.4-D07.6；"
							+ "D17.6；D29；D40；E29；E89.5；F52.4；I86.1；L29.1；"
							+ "N40-N51；Q53-Q55；R86；S31.2-S31.3；Z12.5";
					result.put("errorCode", errorCode);
					result.put("errorItem", errorItem);
					result.put("errorDesc", errorDesc);
				}
			}
			
		}
		if(!result.isEmpty()){
			return result;
		}
		return null;
	}
	
	public static Map<String,String> checkHQMS2(String sexName,String diseaseDiagCode){
		Map<String,String> result =new HashMap<String, String>();
		List<String> diagCodes = new ArrayList<String>();
		diagCodes.add("A34");
		diagCodes.add("B37.3");
		for(int i= 51;i<=58;i++){
			diagCodes.add("C"+i);
		}
		diagCodes.add("C79.6");
		diagCodes.add("D06");
		for(int i= 0;i<=3;i++){
			diagCodes.add("D07."+i);
		}
		
		for(int i= 25;i<=28;i++){
			diagCodes.add("D"+i);
		}
		diagCodes.add("D39");
		diagCodes.add("E28");
		diagCodes.add("E89.4");
		diagCodes.add("F52.5");
		diagCodes.add("F53");
		diagCodes.add("I86.3");
		diagCodes.add("L29.2");
		diagCodes.add("L70.5");
		diagCodes.add("M80.0");
		diagCodes.add("M80.1");
		diagCodes.add("M81.0");
		diagCodes.add("M81.1");
		diagCodes.add("M83.0");
		
		
		for(int i= 70;i<=98;i++){
			diagCodes.add("N"+i);
		}
		
		for(int i= 2;i<=3;i++){
			diagCodes.add("N99."+i);
		}
		
		for(int i= 0;i<=99;i++){
			diagCodes.add("O"+StringUtil.getFormatNumber(i, 2));
		}
		
		diagCodes.add("Q50");
		diagCodes.add("Q51");
		diagCodes.add("Q52");
		diagCodes.add("R87");
		
		diagCodes.add("S31.4");
		for(int i= 4;i<=6;i++){
			diagCodes.add("S37."+i);
		}
		for(int i= 2;i<=3;i++){
			diagCodes.add("T19."+i);
		}
		diagCodes.add("T83.3");
		diagCodes.add("Z01.4");
		
		diagCodes.add("Z12.4");
		diagCodes.add("Z30.1");
		diagCodes.add("Z30.3");
		diagCodes.add("Z30.5");
		diagCodes.add("Z31.1");
		diagCodes.add("Z31.2");
		
		for(int i= 32;i<=36;i++){
			diagCodes.add("Z"+i);
		}
		
		diagCodes.add("Z39");
		diagCodes.add("Z43.7");
		diagCodes.add("Z87.5");
		diagCodes.add("Z97.5");
		if(sexName.contains("男")){
			for (String diagCode : diagCodes) {
				if(diseaseDiagCode.toUpperCase().startsWith(diagCode)){
					String errorCode = "error2";
					String errorItem = "性别（女性）/疾病诊断ICD10编码";
					String errorDesc = "只适用于女性：A34；B37.3；C51-C58；C79.6；D06；D07.0-D07.3；"
							+ "D25-D28；D39；E28；E89.4；F52.5；F53；I86.3；"
							+ "L29.2；L70.5；M80.0-M80.1；M81.0-M81.1；M83.0；"
							+ "N70-N98；N99.2-N99.3；O00-O99；P54.6；Q50-Q52；R87；"
							+ "S31.4；S37.4-S37.6；T19.2-T19.3；T83.3；Z01.4；"
							+ "Z12.4；Z30.1；Z30.3；Z30.5；Z31.1-Z31.2；Z32-Z36；"
							+ "Z39；Z43.7；Z87.5；Z97.5";
					result.put("errorCode", errorCode);
					result.put("errorItem", errorItem);
					result.put("errorDesc", errorDesc);
				}
			}
			
		}
		
		if(!result.isEmpty()){
			return result;
		}
		return null;
	}
	
	public static String checkHQMS3(String sexName,String operateCode){
		List<String> codes = new ArrayList<String>();
		for(int i= 60;i<=64;i++){
			codes.add(""+i);
		}
		codes.add("87.9");
		codes.add("98.24");
		codes.add("99.94");
		if(sexName.contains("女")){
			for (String code : codes) {
				if(operateCode.toUpperCase().startsWith(code)){
					return "只适用于男性：60-64；87.9；98.24；99.94";
				}
			}
			
		}
		return null;
	}
	
	public static String checkHQMS4(String sexName,String operateCode){
		List<String> codes = new ArrayList<String>();
		for(int i= 65;i<=75;i++){
			codes.add(""+i);
		}
		codes.add("87.8");
		codes.add("88.46");
		codes.add("88.78");
		codes.add("89.26");
		
		codes.add("92.17");
		for(int i= 14;i<=18;i++){
			codes.add("96."+i);
		}
		codes.add("97.7");
		for(int i= 16;i<=17;i++){
			codes.add("98."+i);
		}
		if(sexName.contains("男")){
			for (String code : codes) {
				if(operateCode.toUpperCase().startsWith(code)){
					return "只适用于女性：65-75；87.8；88.46；88.78；89.26；"
							+ "92.17；96.14-96.18；97.7；98.16-98.17";
				}
			}
			
		}
		return null;
	}
	
	public static String checkHQMS5(String treatResultCode,String outHospitalTypeCode){
		if(treatResultCode == null){
			return null;
		}
		if(outHospitalTypeCode == null){
			return "离院方式未填写！";
		}
		if("4".equalsIgnoreCase(treatResultCode)){
			if(!"5".equalsIgnoreCase(outHospitalTypeCode)){
				return "诊断治疗结果代码为‘4’，则离院方式为‘5";
			}
		}
		return null;
	}
	
	public static String checkHQMS6(Date birthday,Date inHospitalDateTime,Integer age){
		if(birthday != null){
			if(birthday.after(inHospitalDateTime)){
				return "入院日期应大于出生日期";
			}else{
				Map<String, Integer> dateDiff = DateUtil.getYearMonthDay(birthday, inHospitalDateTime);
				if(dateDiff.get("year") != null){
					int year = dateDiff.get("year");
					if(!(year == age.intValue() || year-1 ==age.intValue())){
						return "年龄=入院日期-出生日期。";
					}
				}else{
					return "年龄=入院日期-出生日期。";
				}
			}
			
		}
		return null;
	}
	
	
	public static String checkHQMS7(Date inHospitalDateTime, Date lastOutHospitalDateTime){
		if(inHospitalDateTime == null){
			return "入院日期不能为空";
		}
		if(lastOutHospitalDateTime != null){
			if(inHospitalDateTime.getTime() < lastOutHospitalDateTime.getTime()){
				return "入院日期应大于等于上次出院日期";
			}
		}
		return null;
	}
	
	public static String checkHQMS8(Date inHospitalDateTime, Date outHospitalDateTime,
			Date diagConfirmedDateTime,Date operateDate){
		if(inHospitalDateTime == null){
			return "入院日期不能为空";
		}
		if(outHospitalDateTime == null){
			return "出院日期不能为空";
		}
		if(inHospitalDateTime.getTime() > outHospitalDateTime.getTime()){
			return "入院日期<=出院日期";
		}
		if(diagConfirmedDateTime != null){
			if(!(diagConfirmedDateTime.getTime() >=inHospitalDateTime.getTime()
					&&outHospitalDateTime.getTime() >= diagConfirmedDateTime.getTime())){
				return "入院日期<=入院后确诊日期<=出院日期";
			}
		}
		if(operateDate != null){
			if(!(operateDate.getTime() >=inHospitalDateTime.getTime()
					&&outHospitalDateTime.getTime() >= operateDate.getTime())){
				return "入院日期<=手术操作日<=出院日期";
			}
		}
		return null;
	}
	
	public static String checkHQMS9(String diagCode){
		if(diagCode != null){
			List<Character> charList = StringUtil.getEnglishLetters('V', 'Y');
			charList.add('M');
			String charListStr = StringUtil.join(charList, ',');
			String firstChar = diagCode.substring(0,1);
			if(charListStr.contains(firstChar)){
				return "门(急)诊,、入院,主要,其它,感染名称不包括M80000/0-M99999/6，及以字母V、W、X、Y开头的编码";
			}
		}
		return null;
	}
	
	public static String checkHQMS10(BigDecimal babyBirthWeight,BigDecimal dayOfAge,String diagCode){
		if(babyBirthWeight != null && diagCode != null){
			if(diagCode.startsWith("P")){
				if(dayOfAge ==null){
					return "当诊断编码(ICD-10)以“P”开头时，则“年龄”以及“年龄不足1周岁(天)”应<=28天";
				}else{
					if(!(dayOfAge.intValue() <=28)){
						return "当诊断编码(ICD-10)以“P”开头时，则“年龄”以及“年龄不足1周岁(天)”应<=28天";
					}
				}
			}
		}
		return null;
	}
	
	public static String checkHQMS11(BigDecimal babyBirthWeight,String diagCode){
		if(babyBirthWeight == null){
			return null;
		}
		if(diagCode != null){
			if(!diagCode.startsWith("Z37")){
				return "所有分娩产妇病历，本次出院其它诊断必须有分娩结局，即Z37编码";
			}
		}
		
		
		return null;
	}
	
	
	public static String checkHQMS12(String pathologyDiagDiagCode){
		if(pathologyDiagDiagCode != null){
			if(!pathologyDiagDiagCode.startsWith("M")){
				return "ICD-10编码范围：M80000/0-M99999/6";
			}
		}
		
		
		return null;
	}
	
	
	public static String checkHQMS13(String mainDiagCode,String damageAndVenenationDiagCode){
		if(mainDiagCode != null){
			List<Character> charList = StringUtil.getEnglishLetters('S', 'T');
			String charListStr = StringUtil.join(charList, ',');
			String firstChar = mainDiagCode.substring(0,1);
			if(charListStr.contains(firstChar) && damageAndVenenationDiagCode == null){
				return "当主要诊断编码(ICD-10)为：S00-T99，则损伤和中毒外部原因编码(ICD-10)、"
						+ "损伤和中毒外部原因名称必填";
			}
		}
		
		
		return null;
	}
	
	public static String checkHQMS14(String damageAndVenenationDiagCode){
		List<Character> charList = StringUtil.getEnglishLetters('V', 'Y');
		String charListStr = StringUtil.join(charList, ',');
		if(damageAndVenenationDiagCode != null){
			String firstChar = damageAndVenenationDiagCode.substring(0,1);
			if(!charListStr.contains(firstChar)){
				return "损伤和中毒外部原因:ICD-10编码范围：V00-Y99";
			}
		}
		return null;
	}
	
	public static String checkHQMS15(String outHospitalTypeCode,Integer rescueNumber,Integer rescueSucceedNumber){
		if(rescueNumber !=null && rescueSucceedNumber ==null){
			return "抢救次数不为空而抢救成功次数为空！";
		}else if(rescueNumber ==null && rescueSucceedNumber !=null){
			return "抢救次数为空而抢救成功次数不为空！";
		}else if(rescueNumber !=null && rescueSucceedNumber !=null){
			if("5".equalsIgnoreCase(outHospitalTypeCode)){
				if(rescueNumber.intValue() > rescueSucceedNumber.intValue()+1 || rescueNumber.intValue() < rescueSucceedNumber.intValue()){
					return "当病人死亡时：抢救次数=抢救成功次数或抢救次数=抢救成功次数+1";
				}
			}else{
				if(rescueNumber.intValue() != rescueSucceedNumber.intValue()){
					return "当病人非死亡时：抢救次数=抢救成功次数";
				}
			}
		}
		
		return null;
	}
	
	public static String checkHQMS16(BigDecimal specialNurseDayNumber,BigDecimal firstLevelNurseDayNumber,
			BigDecimal secondLevelNurseDayNumber,BigDecimal threeLevelNurseDayNumber,
			BigDecimal inHospitalDayNumber){
		if(inHospitalDayNumber == null){
			return "住院天数未填写";
		}
		int nurseDayNumber = 0;
		if(specialNurseDayNumber != null){
			nurseDayNumber +=specialNurseDayNumber.intValue();
		}
		
		if(firstLevelNurseDayNumber != null){
			nurseDayNumber +=firstLevelNurseDayNumber.intValue();
		}
		
		
		if(secondLevelNurseDayNumber != null){
			nurseDayNumber +=secondLevelNurseDayNumber.intValue();
		}
		
		if(threeLevelNurseDayNumber != null){
			nurseDayNumber +=threeLevelNurseDayNumber.intValue();
		}
		
		if(nurseDayNumber >inHospitalDayNumber.intValue()){
			return "实际住院(天)>=特护(天)+一级护理(天)+二级护理(天)+三级护理(天)";
		}
		return null;
	}
	
	public static String checkHQMS17(BigDecimal babyBirthWeight,BigDecimal dayOfAge,String diagCode){
		
		if(babyBirthWeight != null){
			if(diagCode != null){
				if(!diagCode.startsWith("Z37")){
					return "产科分娩病人ICD编码为Z37";
				}
			}else{
				if(dayOfAge == null){
					return "年龄<=28天的新生儿必填";
				}
			}
			
		}
		return null;
	}
	
	
	public static String checkHQMS21(BigDecimal totalCost,List<Map<String,Object>> costRecords){
		
		if(totalCost == null){
			return "住院总费用未填写";
		}
		
		for (Map<String, Object> costRecord : costRecords) {
			
		}
		
		return null;
	}
	
	public static String checkHQMS22(List<Map<String,Object>> costRecords){
		
		for (Map<String, Object> costRecord : costRecords) {
			
		}
		return null;
	}
	
}
